home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST11-12.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  2KB  |  79 lines

  1. ;
  2. ; *** Listing 11-12 ***
  3. ;
  4. ; Finds the first occurrence of the letter 'z' in
  5. ; a zero-terminated string, using REPNZ SCASB in a
  6. ; double-search approach, first finding the terminating
  7. ; zero to determine the string length, and then searching
  8. ; for the desired byte.
  9. ;
  10.     jmp    Skip
  11. ;
  12. TestString    label    byte
  13.     db    'This is a test string that is '
  14.     db    'z'
  15.     db    'terminated with a zero byte...',0
  16. ;
  17. ; Finds the first occurrence of the specified byte in the
  18. ; specified zero-terminated string.
  19. ;
  20. ; Input:
  21. ;    AL = byte to find
  22. ;    DS:SI = zero-terminated string to search
  23. ;
  24. ; Output:
  25. ;    SI = pointer to first occurrence of byte in string,
  26. ;        or 0 if the byte wasn't found
  27. ;
  28. ; Registers altered: AH, CX, SI, DI, ES
  29. ;
  30. ; Direction flag cleared
  31. ;
  32. ; Note: Do not pass a string that starts at offset 0 (SI=0),
  33. ;    since a match on the first byte and failure to find
  34. ;    the byte would be indistinguishable.
  35. ;
  36. ; Note: If the search value is 0, will not find the
  37. ;    terminating zero in a string that is exactly 64K
  38. ;    bytes long. Does not handle strings that are longer
  39. ;    than 64K bytes or cross segment boundaries.
  40. ;
  41. FindCharInString:
  42.     mov    ah,al    ;set aside the byte to be found
  43.     sub    al,al    ;we'll search for zero
  44.     push    ds
  45.     pop    es
  46.     mov    di,si    ;SCAS uses ES:DI
  47.     mov    cx,0ffffh ;long enough to handle any string
  48.             ; up to 64K-1 bytes in length, and
  49.             ; will handle 64K case except when
  50.             ; the search value is the terminating
  51.             ; zero
  52.     cld
  53.     repnz    scasb    ;find the terminating zero
  54.     not    cx    ;length of string in bytes, including
  55.             ; the terminating zero except in the
  56.             ; case of a string that's exactly 64K
  57.             ; long including the terminating zero
  58.     mov    al,ah    ;get back the byte to be found
  59.     mov    di,si    ;point to the start of the string again
  60.     repnz    scasb    ;search for the byte of interest
  61.     jnz    FindCharInStringNotFound
  62.             ;the byte isn't present in the string
  63.     dec    di    ;we've found the desired value. Point
  64.             ; back to the matching location
  65.     mov    si,di    ;return the pointer in SI
  66.     ret
  67. FindCharInStringNotFound:
  68.     sub    si,si    ;return a 0 pointer indicating that
  69.             ; no match was found
  70.     ret
  71. ;
  72. Skip:
  73.     call    ZTimerOn
  74.     mov    al,'z'        ;byte value to find
  75.     mov    si,offset TestString
  76.                 ;string to search
  77.     call    FindCharInString ;search for the byte
  78.     call    ZTimerOff
  79.